function generateMetaNodePNG_polygs(geneID,geneSymbol,dataExpr,dataFlux,dataTF,dataK4,dataConf,rxn,plotDir)
% function generateMetaNodePNG_polygs(geneID,geneSymbol,dataExpr,dataFlux,dataTF,dataK4,dataConf,rxn,plotDir)
% USAGE: 
% generateMetaNodePNG_polygs() %%% - generates a default example metanode
% generateMetaNodePNG_polygs(1000,'ABC1',[-1 0 1 0 -1 1 1],[-1 0 1 2 3 1 1],[1 0 1],[0 1],[-1 0 1 2 0 1 1],'rxnABC1','Metanode_Example')
% %%%generates a metanode with specified features
% REQUIRES: export_fig.m & print2array.m [Oliver Woodford 2008-2012, downloaded from Matlab central 18.4.12]
% INPUTS:
% geneID - Entrez gene ID; numeric;
% geneSymbol - gene symbol; string;
% dataExpr - expression data that will fill the lower row of the rectangle; numeric; length = 7
% dataFlux - reaction activity prediction data will fill the upper row of the rectangle; numeric; length = 7.
% dataTF - TF binding binary (0 or 1) data; for filling or not right side polygons; numeric; length = 3.
% dataK4 - H3K4me3 trinary data; for filling or not the 2 circles above the reactangle; numeric; length = 2.
% dataConf - prediction confidence, same length as dataFlux; numeric;% length = 7;
% rxn - reaction name/abbreviation; string.
% plotDir - name of directory where png file will be generated; string.
% Color coding:
% Expression
% -1 - BLUE (lowly expressed)
% 0 - GREY (moderately expressed)
% 1 - RED (highly expressed)
% 2 - WHITE (gene in model not found in data - no gene expression)
% Flux
% -1 - YELLOW (active reverse way)
% 0 - BLUE (inactive)
% 1 - RED (active direct way)
% 2 - ORANGE (active unknown way)
% 3 - GREY (undetermined + predicted w/o confidence)
% TFs (PPG star; CEA triangle; LXR rectangle)
% absent - WHITE
% present - RED
% K4me3
% -1 - WHITE (absent)
% 0 - GREY
% 1 - RED (present)
%
% Thomas Sauter, University of Luxembourg, April 20, 2012
if ~exist(['geneID','geneSymbol','dataExpr','dataFlux','dataTF','dataK4','dataConf','rxn','plotDir'],'var')
geneID=12345;
geneSymbol='ABCDE';
dataExpr=[-1 -1 0 0 1 1 1];
dataFlux=[0 0 3 2 -1 1 1];
dataTF=[1 1 1];
dataK4=[0 1];
dataConf=[1 1 1 1 1 1 1 1];
rxn='rxnExample';
mkdir(pwd,'MetanodeExample')
plotDir=[pwd '\MetanodeExample'];
end

figure
daspect([1,1,1])
if length(dataExpr)==5
    plotWidth=10;
elseif length(dataExpr)==6
    plotWidth=8;
else
    plotWidth=7;
end
%%used in order to have nice graph spacing
if ~isnan(dataFlux) %if not NaN (= if not blocked rxn)
    % Expression %%LOWER LINE (in the main rectangle!)
    for counter=1:length(dataExpr)
        h=rectangle('Position',[(counter-1)*plotWidth,0,plotWidth,10],'LineWidth',3);
        if dataExpr(counter)==-1 %lowly expressed
            set(h,'FaceColor','b'); %blue
        elseif dataExpr(counter)==0 %moderately expressed
            set(h,'FaceColor',[0.7 0.7 0.7]); %grey
        elseif dataExpr(counter)==1 %highly expressed
            set(h,'FaceColor','r'); %red
        elseif dataExpr(counter)==2 %gene in model w/o expression in data
            set(h,'FaceColor','w'); %white
        end
        set(gca,'XTick',[],'YTick',[]);
    end
    
    % Flux
    for counter=1:length(dataFlux) %%UPPER LINE
        h=rectangle('Position',[(counter-1)*plotWidth,10,plotWidth,10],'LineWidth',3);
        if dataFlux(counter)==-1 &(dataConf(counter)~=0) %(active reverse way)
            set(h,'FaceColor','y') %%yellow
        elseif dataFlux(counter)==0 &(dataConf(counter)~=0) %(inactive)
            set(h,'FaceColor','b')%%blue
        elseif dataFlux(counter)==1 &(dataConf(counter)~=0) %(active direct way)
            set(h,'FaceColor','r')%%red
        elseif dataFlux(counter)==2 &(dataConf(counter)~=0) %(active unknown way)
            set(h,'FaceColor',[1 0.5 0.2])%%orange?
        elseif dataFlux(counter)==3 &(dataConf(counter)~=0) %(undetermined + predicted w/o confidence)
            set(h,'FaceColor',[0.7 0.7 0.7])%%grey
        elseif dataFlux(counter)==5
            set(h,'FaceColor','w')%%white (4 genes associated white multiple rxns, metanode icon in cytsocapeWeb is shown in white!)
        else
            set(h,'FaceColor',[0.7 0.7 0.7])%%grey
        end
        set(gca,'XTick',[],'YTick',[])
    end
    
    
    % K4
    if ~max(isnan(dataK4))
        %     h=rectangle('Position',[1,22,6,6],'Curvature',[1,1],'LineWidth',4);
        x=sin(0:0.01:(2*pi))*3;
        y=cos(0:0.01:(2*pi))*3;
        x=x+4;
        y=y+25; %%!!
        h=patch(x,y,'w','LineWidth',4);
        %     set(h,'LineWidth',4)
        if dataK4(1)==1
            set(h,'FaceColor','r','LineWidth',4) %%red
        elseif dataK4(1)==0
            set(h,'FaceColor',[0.7 0.7 0.7],'LineWidth',4) %%grey
        end
        %     h=rectangle('Position',[42,22,6,6],'Curvature',[1,1],'LineWidth',4);
        x=sin(0:0.01:(2*pi))*3;
        y=cos(0:0.01:(2*pi))*3;
        x=x+45;
        y=y+25; %%!!
        h=patch(x,y,'w','LineWidth',4);
        %     set(h,'LineWidth',4)
        if dataK4(2)==1
            set(h,'FaceColor','r','LineWidth',4) %%red
        elseif dataK4(2)==0
            set(h,'FaceColor',[0.7 0.7 0.7],'LineWidth',4) %%grey
        end
    end
    
    % TF
    if ~isempty(dataTF)
        %%LXR
        h=rectangle('Position',[52,1,6,6],'Curvature',[0,0],'LineWidth',4); %%LXR - RECTANGLE
        set(h,'FaceColor',[1 1 1]) %set default color as white (1 1 1 in rgb code)
        if dataTF(3)==1%%LXR - RECTANGLE
            set(h,'FaceColor','r')
        end
        
        %%CEA
        % h=rectangle('Position',[52,8,6,6],'Curvature',[1,1],'LineWidth',4); %%CEA
        x=[0 6 3]; %%6x6! equilat. triangle
        y=[0 0 6];
        x=x+52;
        y=y+9; %%!!
        h=patch(x,y,'w');
        set(h,'LineWidth',4)
        if dataTF(2)==1%%CEA - TRIANGLE
            set(h,'FaceColor','r')
        end
        
        % % % %%PPG - star
        % h=rectangle('Position',[52,15,6,6],'Curvature',[1,1],'LineWidth',4); %%PPG
        x=[0 3 6 4.5 6 4 3 2 0 1.5]; %%6x6!
        y=[0 2 0 3 4.5 4.5 6 4.5 4.5 3];
        
        x=x+52;
        y=y+15; %%!!
        h4=patch(x,y,'w','LineWidth',4);
        if dataTF(1)==1%%PPG - STAR
            set(h4,'FaceColor','r')
        end
        h=text(26,-7,geneSymbol,'FontSize',60,'HorizontalAlignment','center');%1
    end
    
else %if flux = NaN (blocked rxn, gene info only, change place of elements in the figure)
    % Expression %%LOWER LINE (in the main rectangle!)
    for counter=1:length(dataExpr)
        h=rectangle('Position',[(counter-1)*plotWidth,0,plotWidth,10],'LineWidth',3);
        if dataExpr(counter)==-1 %lowly expressed
            set(h,'FaceColor','b') %blue
        elseif dataExpr(counter)==0 %moderately expressed
            set(h,'FaceColor',[0.7 0.7 0.7]) %grey
        elseif dataExpr(counter)==1 %highly expressed
            set(h,'FaceColor','r') %red
        elseif dataExpr(counter)==2 %gene in model w/o expression in data
            set(h,'FaceColor','w') %white
            
        end
        set(gca,'XTick',[],'YTick',[])
        h=text(26,-7,geneSymbol,'FontSize',60,'HorizontalAlignment','center');%1
    end
    % K4
    if ~max(isnan(dataK4))
        %     h=rectangle('Position',[1,22,6,6],'Curvature',[1,1],'LineWidth',4);
        x=sin(0:0.01:(2*pi))*3;
        y=cos(0:0.01:(2*pi))*3;
        x=x+4;
        y=y+15; %%!!
        h=patch(x,y,'w','LineWidth',4);
        %     set(h,'LineWidth',4)
        if dataK4(1)==1
            set(h,'FaceColor','r','LineWidth',4) %%red
        elseif dataK4(1)==0
            set(h,'FaceColor',[0.7 0.7 0.7],'LineWidth',4) %%grey
        end
        %     h=rectangle('Position',[42,22,6,6],'Curvature',[1,1],'LineWidth',4);
        x=sin(0:0.01:(2*pi))*3;
        y=cos(0:0.01:(2*pi))*3;
        x=x+45;
        y=y+15; %%!!
        h=patch(x,y,'w','LineWidth',4);
        %     set(h,'LineWidth',4)
        if dataK4(2)==1
            set(h,'FaceColor','r','LineWidth',4) %%red
        elseif dataK4(2)==0
            set(h,'FaceColor',[0.7 0.7 0.7],'LineWidth',4) %%grey
        end
    end
    
    % TF
    if ~isempty(dataTF)
        %%LXR
        h=rectangle('Position',[52,-2,6,6],'Curvature',[0,0],'LineWidth',4); %%LXR - RECTANGLE
        set(h,'FaceColor',[1 1 1]) %set default color as white (1 1 1 in rgb code)
        if dataTF(3)==1%%LXR - RECTANGLE
            set(h,'FaceColor','r')
        end
        
        %%CEA
        % h=rectangle('Position',[52,8,6,6],'Curvature',[1,1],'LineWidth',4); %%CEA
        x=[0 6 3]; %%6x6! equilat. triangle
        y=[0 0 6];
        x=x+52;
        y=y+6; %%!!
        h=patch(x,y,'w');
        set(h,'LineWidth',4)
        if dataTF(2)==1%%CEA - TRIANGLE
            set(h,'FaceColor','r')
        end
        
        % % % %%PPG - star
        % h=rectangle('Position',[52,15,6,6],'Curvature',[1,1],'LineWidth',4); %%PPG
        x=[0 3 6 4.5 6 4 3 2 0 1.5]; %%6x6!
        y=[0 2 0 3 4.5 4.5 6 4.5 4.5 3];
        
        x=x+52;
        y=y+12; %%!!
        h4=patch(x,y,'w','LineWidth',4);
        if dataTF(1)==1%%PPG - STAR
            set(h4,'FaceColor','r')
        end
        
    end
end
%general
set(gca, 'color', 'none','Visible','off')
set(gcf, 'color', 'none')
% % % % printFILE=[plotDir '\' gene '_rxn' num2str(rxn) '.png'];
% printFILE=[plotDir '\' num2str(geneID) '_' geneSymbol '_rxn_' rxn '.png']; %png
%clean rxn name from '/','&','\',':','|','*','?','"','>','<':, replace by '-'
pat='(/)|(\)|(&)|(|)|(*)|(?)|(:)|(")|(<)|(>)';
rxn=regexprep(rxn,pat,'-');
if ~isempty(geneID)
    printFILE=[plotDir '\' num2str(geneID) '_' rxn '.png']; %png %%%e.g. 19_Cholest_t
else
    printFILE=[plotDir '\' rxn '.png']; %png %%%e.g. 19_Cholest_t
end
mkdir(plotDir)
eval(['export_fig ' '''' printFILE '''' ' -transparent'])
disp(' ')
disp(['Metanode png for ' geneSymbol ' generated.'])
disp(' ')
close('all')
end
